package com.hengxinguotong.zhihuichengjian.ui.quality;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.MeasureListAdapter;
import com.hengxinguotong.zhihuichengjian.ui.app.BaseFragment;
import com.hengxinguotong.zhihuichengjian.bean.Measure;
import com.hengxinguotong.zhihuichengjian.bean.MeasureListRes;
import com.hengxinguotong.zhihuichengjian.utils.SPUtil;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshBase;
import com.hengxinguotong.zhihuichengjian.widget.pullrefresh.PullToRefreshListView;
import com.lidroid.xutils.http.RequestParams;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/12/18.
 */

public class MeasureFragment extends BaseFragment {
    @BindView(R.id.measure_lv)
    PullToRefreshListView measureLv;
    Unbinder unbinder;
    private int type; //1模板；2钢筋；3砼；4砌筑；5抹灰
    private int pageIndex = 1;
    private List<Measure> measureList;
    private MeasureListAdapter adapter;
    private MeasureListActivity mActivity;
    private Handler handler = new Handler();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.fragment_measure, null);
        unbinder = ButterKnife.bind(this, contentView);
        mActivity = (MeasureListActivity) getActivity();
        type = getArguments().getInt("type", 1);
        measureLv.setMode(PullToRefreshBase.Mode.BOTH);
        measureLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                getMeasureList(null);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                getMeasureList(null);
            }
        });
//        measureLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//            }
//        });
        return contentView;
    }

    @Override
    public void onLazyLoad() {
        handler.postDelayed(MyRunnable, 500);

    }

    Runnable MyRunnable = new Runnable() {
        @Override
        public void run() {
            measureLv.setRefreshing();
        }
    };
    @Override
    public void onStart() {
        super.onStart();
    }

    public static MeasureFragment newInstance(int type) {
        MeasureFragment fragment = new MeasureFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    private void getMeasureList(String alert) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("constructionId", SPUtil.getString(mActivity,"constructionid"));
            jsonObject.put("pageNum", pageIndex + "");
            jsonObject.put("type", type + "");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestParams params = new RequestParams();
        params.addHeader("Content-type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Utils.requestData(alert, mActivity, "/inspection/measure/list/" + SPUtil.getString(mActivity,"uuid"), params, new Utils.ListenerJson() {
            @Override
            public void onSuccess(String json) {
                if (measureLv.isRefreshing()) {
                    measureLv.onRefreshComplete();
                }
                if (!Utils.isEmpty(json)) {
                    Gson gson = new Gson();
                    MeasureListRes measureListRes = gson.fromJson(json, MeasureListRes.class);
                    process(measureListRes.getValue());
                }else{
                    showEmptyView(measureLv);
                }
            }

            @Override
            public void onFailure(String json) {
                if (measureLv.isRefreshing()) {
                    measureLv.onRefreshComplete();
                }
            }
        });
    }

    private void process(List<Measure> datas){
        if (pageIndex == 1) {
            if(datas.size()==0){
                showEmptyView(measureLv);
            }else{
                dismissEmptyView();
            }
            measureList = datas;
            adapter = new MeasureListAdapter(measureList, mActivity);
            measureLv.setAdapter(adapter);
            if (datas.size() == Utils.ONCECOUNT) {
                measureLv.setMode(PullToRefreshBase.Mode.BOTH);
            }
        } else {
            measureList.addAll(datas);
            adapter.notifyDataSetChanged();
        }

        adapter.setType(type);

        if (datas.size() < Utils.ONCECOUNT) {
            measureLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        }

        if (datas.size() != 0) {
            pageIndex++;
        }
    }

    public void reLoad(){
        handler.postDelayed(MyRunnable, 500);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

}
